package com.ra.controller;


import com.ra.pojo.RespBean;
import com.ra.pojo.RespPageBean;
import com.ra.pojo.dto.CategoryAdminDTO;
import com.ra.pojo.dto.CategoryDTO;
import com.ra.pojo.dto.ConditionDTO;
import com.ra.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@RestController
@Api(tags = "文章分类模块")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;


    @ApiOperation(value = "查看后台分类列表")
    @GetMapping("/admin/categories")
    public RespBean<RespPageBean<CategoryAdminDTO>> listCategoriesAdmin(ConditionDTO conditionDTO) {
        return RespBean.success(categoryService.listCategoriesAdmin(conditionDTO));
    }
    @ApiOperation(value = "搜索文章分类")
    @GetMapping("/admin/categories/search")
    public RespBean<List<CategoryDTO>> listCategoriesAdminBySearch(ConditionDTO conditionDTO) {
        return RespBean.success(categoryService.listCategoriesBySearch(conditionDTO));
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/admin/categories")
    public RespBean<?> delCategories(@RequestBody List<Integer> categoryIds) {
        categoryService.delCategories(categoryIds);
        return RespBean.success();
    }


    @ApiOperation(value = "添加或修改分类")
    @PostMapping("/admin/categories")
    public RespBean<?> saveOrUpdateCategory(@Valid @RequestBody CategoryDTO categoryDTO) {
        categoryService.saveOrUpdateCategory(categoryDTO);
        return RespBean.success();
    }

}

